<template>
  <div id="app">
    <h2>---------模块状态----------</h2>
    <p>{{ $store.state.qmq.turnip }}</p>
    <button @click="mt">模块对象</button>
    <h2>--------基本操作-----</h2>
    <h3>{{ message }}</h3>
    <h3>{{ $store.state.count }}</h3>
    <button @click="add">+</button>
    <button @click="sub">-</button>
    <button @click="addCount(5)">+5</button>
    <button @click="addCount(10)">+10</button>
    <button @click="addStu()">新增一个指定的学生</button>
    <h3>{{ $store.state.user }}</h3>
    <button @click="updateInfo()">修改信息</button>
    <h3>异步修改的信息:{{ $store.state.user }}</h3>
    <button @click="aUpdateInfo()">异步修改信息</button>
    <h3>{{ stuCount }}</h3>
    <h3>{{ stuById }}</h3>
    <h2>--------------HelloVuex---------</h2>
    <hello-vuex />
  </div>
</template>

<script>
import HelloVuex from "./components/HelloVuex";
import { UPDATEINFO } from "./store/mutation-type";

export default {
  name: "App",
  data() {
    return {
      message: "",
    };
  },
  components: {
    HelloVuex,
  },
  computed: {
    stuCount() {
      // return this.$store.state.students.filter(student => student.age > 20).length
      return this.$store.getters.getStudentCounts;
    },
    stuById() {
      return this.$store.getters.getStuById(110);
    },
  },
  methods: {
    mt() {
      console.log(this.$store.state.qmq);
    },
    add() {
      this.$store.commit("increment");
    },
    sub() {
      this.$store.commit("decrement");
    },
    addCount(count) {
      // this.$store.commit('addCount', count)
      //第二种方式
      this.$store.commit({
        type: "addCount",
        count,
      });
    },
    addStu() {
      const stu = {
        id: 114,
        name: "ytz",
        age: "35",
      };
      this.$store.commit("addStu", stu);
    },
    [UPDATEINFO]() {
      this.$store.commit(UPDATEINFO, 12);
    },
    aUpdateInfo() {
      // 经过acrion，即触发action内的函数,并传入数据lisi
      // this.$store
      //   .dispatch("aUpdateInfo", { name: "turnip", age: 13 })
      //   .then((response) => {
      //     console.log(response);
      //   });
      // 第二种传递方法
      this.$store
        .dispatch({
          type: "aUpdateInfo",
          data: { name: "turnip", age: 13 },
        })
        .then((response) => {
          console.log(response);
        });
    },
  },
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
